package com.happypets.servicesImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.happypets.daoImpl.ManagerDaoImpl;
import com.happypets.model.ManagerBean;
import com.happypets.services.ManagerService;

public class ManagerServiceImpl implements ManagerService {

	ManagerDaoImpl mdi = new ManagerDaoImpl();
	
	@Override
	public List<ManagerBean> allManagers(String pageNumber,String  showNumber) {
		
		List<ManagerBean> lmb = new ArrayList<ManagerBean>();
		
		String sql1 = "select * from (select t.*,rownum as rowno from managerinfo t where rownum<="+(Integer.parseInt(pageNumber)*Integer.parseInt(showNumber))+" ) where rowno > "+((Integer.parseInt(pageNumber)-1)*Integer.parseInt(showNumber));
		
		List<Map<String,String>> lm = mdi.query(sql1);
		
		for (int i = 0; i < lm.size(); i++) {
			
			Map<String,String> mi = lm.get(i);
			
			String id = mi.get("MANAGER_ID");
			
			String sql2 = "select a.power,b.power_name from power a left join power_name b on a.power=b.power_id where a.managerid ="+id;
			List<Map<String,String>> powers = mdi.query(sql2);
			List<String> ps = listToIdList(powers);
			
			ManagerBean mb = turnToManagerBean(mi,ps);
			
			lmb.add(mb);
		}
		
		return lmb;
	}

	@Override
	public ManagerBean managerInfo(String id) {
		
		String sql = "select t.* from managerinfo t where t.manager_id="+id;
		
		Map<String,String> lm = mdi.query(sql).get(0);
		
		sql = "select a.power,b.power_name from power a left join power_name b on a.power=b.power_id where a.managerid = "+id;
		
		List<String> powers = listToIdList(mdi.query(sql));
		
		return turnToManagerBean(lm,powers);
	}
	
	
	/**
	 * 权限存储方式转换
	 * @param powers
	 * @return
	 */
	public List<String> listToIdList(List<Map<String,String>> powers){
		List<String> li = new ArrayList<String>();
		for (int i = 0; i < powers.size(); i++) {
			li.add(powers.get(i).get("POWER_NAME"));
		}
		
		return li;
		
	}
	
	/**
	 * 转化成bean
	 * @param si
	 * @param powers
	 * @return
	 */
	public ManagerBean turnToManagerBean(Map<String,String> si,List<String> powers){
		ManagerBean mana = new ManagerBean();
		
		mana.setId(si.get("MANAGER_ID"));
		mana.setName(si.get("MANAGER_NAME"));
		mana.setPasswd(si.get("MANAGER_PASSWORD"));
		mana.setPower(powers);
		return mana;
	}

	@Override
	public List<ManagerBean> allManagers() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int deleteManager(String id){
		
		String[] sqls = new String[2];
		
		sqls[0] = "delete from managerinfo t where t.manager_id="+id;
		
		sqls[1] = "delete from power p where p.managerid="+id;
		
		System.out.println(sqls[0]);
		System.out.println(sqls[1]);

		
		return mdi.prepareExcuteSql(sqls)[0];
		
	}
	
	
	public int addManager(ManagerBean mb){
		
		String name = mb.getName();
		String password = mb.getPasswd();
		List<String> ps = mb.getPower();
		

		String sql = "insert into managerinfo(manager_id,manager_name,manager_password) values(managerinfo_seq.nextval,'"+name+"','"+password+"')";
		
		mdi.excuteSql(sql);
		
		//执行完才能获取到id，进行权限设置
		
		if(ps == null){
			return 1;
		}
		String[] sqls = new String[ps.size()];
		UserServiceImpl psi = new UserServiceImpl();
		String id = psi.backstageUserLogin(name, password);
//		delete from power p where p.managerid='3' 
//
//				insert into power(managerid,power) values('1','1')
		
		
		for(int i=0;i<ps.size();i++){
			sqls[i] = "insert into power(managerid,power) values("+id+","+ps.get(i)+")";
			System.out.println(sqls[i]);
		}
		
		
		return mdi.prepareExcuteSql(sqls)[0];
		
	}
	
	public int updateManager(ManagerBean mb){
		
		String id = mb.getId();
		String name = mb.getName();
		String password = mb.getPasswd();
		List<String> ps = mb.getPower();
		String[] sqls;
		if(ps == null){
			sqls = new String[2];
		}else{
			sqls = new String[ps.size()+2];
		}
		
		
		//删除manager
//		update managerinfo t set t.manager_password='',t.manager_name='' where t.manager_id=''
//
//				delete from power p where p.managerid='3' 
//
//				insert into power(managerid,power) values('1','1')
		
		sqls[0] = "update managerinfo t set t.manager_password='"+password+"',t.manager_name='"+name+"' where t.manager_id="+id;
		
		sqls[1] = "delete from power p where p.managerid="+id;
		
		if(ps != null){
			for(int i=0;i<ps.size();i++){
				sqls[i+2] = "insert into power(managerid,power) values("+id+","+ps.get(i)+")";
				System.out.println(sqls[i+2]);
			}
		}

		return mdi.prepareExcuteSql(sqls)[0];
		
	}

}
